/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef BINARYOPNODE_H
#define BINARYOPNODE_H

#include <SmartPtr.h>
#include <ExpressionTreeNode.h>

class BinaryOpNode : public ExpressionTreeNode
{
public:
    BinaryOpNode(const QString& info, MathBinaryFun fun = 0, ExpressionTreeOp derivativeBuilder = 0);

    virtual ~BinaryOpNode();

    virtual Real eval() const;

    virtual SmartPtr<ExpressionTreeNode> derive(const QString& varName) const;

    /**
     *\fn QString info() const
     *\brief Returns the information contained by the node
     * That could be the symbol of the operand, the variable name or the value as a string
     * Reimplemented in derived classes
     */
    inline virtual QString info() const {
        return _info;
    }

    /**
     *\fn SmartPtr<ExpressionTreeNode> left()
     *\brief Returns a pointer to the left subtree root
     * Reimplemented in derived classes
     *
     */
    inline virtual SmartPtr<ExpressionTreeNode> left() {
        return _left;
    }

    /**
     *\fn const  SmartPtr<ExpressionTreeNode> left() const
     *\brief Returns a constant pointer to the left subtree root
     * Reimplemented in derived classes
     *
     */
    inline virtual const SmartPtr<ExpressionTreeNode> left() const {
        return _left;
    }

    /**
     *\fn void setLeft(SmartPtr<ExpressionTreeNode> node)
     *\brief Assigns the left subtree to the tree having as root the argument passed to the method
     *\param node A pointer to the root of the new left subtree
     * If the left subtree is not null it will be  previously destroyed and memory freed
     */
    inline virtual void setLeft(SmartPtr<ExpressionTreeNode> node)  throw (OperandsException) {
        _left = node;
    }

    /**
     *\fn SmartPtr<ExpressionTreeNode> right()
     *\brief Returns a pointer to the right subtree root
     * Reimplemented in derived classes
     *
     */
    inline virtual SmartPtr<ExpressionTreeNode> right() {
        return _right;
    }

    /**
     *\fn const SmartPtr<ExpressionTreeNode> right() const
     *\brief Returns a constant pointer to the right subtree root
     */
    inline virtual const SmartPtr<ExpressionTreeNode> right() const {
        return _right;
    }

    /**
     *\fn void setRight(SmartPtr<ExpressionTreeNode> node)
     *\brief Assigns the right subtree to the tree having as root the argument passed to the method
     *\param node A pointer to the root of the new right subtree
     * If the right subtree is not null it will be  previously destroyed and memory freed
     */
    inline virtual void setRight(SmartPtr<ExpressionTreeNode>  node)  throw (OperandsException) {
        _right = node;
    }

    /**
     *\fn bool isBinaryOp() const
     *\brief Returns true if the node is a binary operator
     * Reimplemented in derived classes
     *
     */
    inline virtual bool isBinaryOp() const {
        return true;
    }

    virtual QString toPrefixString() const;

    virtual QString toPostfixString() const;

    virtual QString toInfixString() const;

    virtual void bind(int varPos,  const Real * var) const;

    virtual void bind(const QString& varName,  const Real * var) const;

    virtual  ExpressionTreeNode* replace(const QString& varName, ExpressionTreeNode* tree) const;

    virtual  ExpressionTreeNode* replace(int varPos, ExpressionTreeNode* tree) const;

    virtual SmartPtr<ExpressionTreeNode> firstLevelSimplification() const =0;

    static ExpressionTreeNode* makeNew(const QString& info);

    virtual void resetMarker() const;

protected:
    SmartPtr<ExpressionTreeNode>  _left;
    SmartPtr<ExpressionTreeNode>  _right;
    MathBinaryFun _op;
    ExpressionTreeOp _derivativeBuilder;
    QString _info;
};

#endif // BINARYOPNODE_H
